package com.hemaapp.yjnh.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.yjnh.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 带有加减、数量可输入的数量选择器
 * Created by UU on 2016/9/20.
 */
public class NumberSelectorLayout extends LinearLayout implements View.OnClickListener {

    TextView tvSub;
    EditText edCount;
    TextView tvPlus;

    boolean mEditable = false;//是否可手动输入
    int mSubPlusColor;
    int mNumber = 1;
    int maxValue = 100;
    int minValue = 0;
    int mStep = 1;//每加一次或者减一次变化的值
    NumberSelectorListener mListener;

    public NumberSelectorLayout(Context context) {
        super(context);
    }

    public NumberSelectorLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public NumberSelectorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater layoutInflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View container = layoutInflater.inflate(R.layout.layout_number_selector, this, true);

        tvSub = (TextView) container.findViewById(R.id.tv_sub);
        edCount = (EditText) container.findViewById(R.id.ed_count);
        tvPlus = (TextView) container.findViewById(R.id.tv_plus);
        tvSub.setOnClickListener(this);
        edCount.setOnClickListener(this);
        tvPlus.setOnClickListener(this);
        TypedArray typedArray = context.obtainStyledAttributes(attrs
                , R.styleable.NumberSelector);
        mEditable = typedArray.getBoolean(R.styleable.NumberSelector_editable, false);
        mSubPlusColor = typedArray.getColor(R.styleable.NumberSelector_sub_plus_color, 0xffffae40);
        mNumber = typedArray.getInteger(R.styleable.NumberSelector_number, 1);
        mStep = typedArray.getInteger(R.styleable.NumberSelector_step, 1);
        typedArray.recycle();
        if (mNumber <= 1)
            tvSub.setEnabled(false);
        else
            tvSub.setEnabled(true);
        tvSub.setTextColor(mSubPlusColor);
        tvPlus.setTextColor(mSubPlusColor);
        edCount.setText(mNumber+"");
        edCount.setEnabled(mEditable);
        edCount.addTextChangedListener(new TextWatcher() {
            private boolean isChanged = false;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (isChanged) {
                    return;
                }
                isChanged = true;
                String buyCount = s.toString().trim();
                if (buyCount.length() == 0) {
                    tvSub.setEnabled(false);
                    mNumber = minValue;
                    if (mListener != null)
                        mListener.onNumberChanged(mNumber);
                    isChanged = false;
                    return;
                }
                while (buyCount.length() > 1 && buyCount.startsWith("0")) {
                    buyCount = buyCount.substring(1);
                }
                mNumber = Integer.parseInt(buyCount);
                if (mNumber > maxValue) {
                    mNumber = maxValue;
                    tvSub.setEnabled(true);
                    tvPlus.setEnabled(false);
                }else if (mNumber < minValue) {
                    mNumber = minValue;
                    tvSub.setEnabled(false);
                    tvPlus.setEnabled(true);
                }else {
                    tvSub.setEnabled(true);
                    tvPlus.setEnabled(true);
                }
                int selection = edCount.getSelectionEnd();//必须要放到settext之前
                edCount.setText(mNumber+"");
//                edCount.setSelection(edCount.length());
                if (selection < edCount.length())
                    edCount.setSelection(selection);
                else
                    edCount.setSelection(edCount.length());
                if (mListener != null)
                    mListener.onNumberChanged(mNumber);
                isChanged = false;
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_sub://减
                mNumber -= mStep;
                edCount.setText(mNumber+"");
                break;
            case R.id.tv_plus://加
                mNumber += mStep;
                edCount.setText(mNumber+"");
                break;
        }
    }

    public void setNumberSelectorListener(NumberSelectorListener listener) {
        this.mListener = listener;
    }

    public void setEditable(boolean editable) {
        this.mEditable = editable;
        edCount.setEnabled(mEditable);
    }

    public void setmSubPlusColor(int subPlusColor) {
        this.mSubPlusColor = subPlusColor;
        tvPlus.setTextColor(subPlusColor);
        tvSub.setTextColor(subPlusColor);
    }

    public void setNumber(int number) {
        this.mNumber = number;
        edCount.setText(number+"");

        if (mNumber > maxValue) {
            mNumber = maxValue;
            tvSub.setEnabled(true);
            tvPlus.setEnabled(false);
        }else if (mNumber < minValue) {
            mNumber = minValue;
            tvSub.setEnabled(false);
            tvPlus.setEnabled(true);
        }else {
            tvSub.setEnabled(true);
            tvPlus.setEnabled(true);
        }
    }

    public int getNumber() {
        return mNumber;
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
        if (maxValue <= 0) {
            this.maxValue = 0;
            setMinValue(0);
        }
        if (this.maxValue < minValue)
            this.maxValue = minValue;
        if (mNumber > maxValue) {
            mNumber = maxValue;
            edCount.setText(mNumber+"");
        }
    }

    public void setMinValue(int minValue) {
        this.minValue = minValue;
        if (mNumber < minValue) {
            mNumber = minValue;
            edCount.setText(mNumber+"");
        }
    }

    public void setPlusEnable(boolean enable) {
        tvPlus.setEnabled(enable);
    }

    public void setSubEnable(boolean enable) {
        tvSub.setEnabled(enable);
    }
    public interface NumberSelectorListener {
        public void onNumberChanged(int curValue);
    }
}
